package com.yyds.blackmagic.service.impl;

import com.yyds.blackmagic.entity.Book;
import com.yyds.blackmagic.entity.Category;
import com.yyds.blackmagic.mapper.BookMapper;
import com.yyds.blackmagic.mapper.CategoryMapper;
import com.yyds.blackmagic.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.annotation.Retention;
import java.util.List;

/**
 * @ClassName BookServiceImpl
 * @Auther Dell
 * @Date 2021/12/13 17:17
 **/
@Service
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper bookMapper;
    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public List<Book> selectAll() {
        List<Book> books = bookMapper.selectAll();
        for (Book book : books) {
            Category one = categoryMapper.findOne (book.getCid ());
            book.setCategory (one);
        }
        return books;
    }

    @Override
    public  List<Book> selectOne(int cid) {
        List<Book> list = bookMapper.selectOne(cid);
        return list;
    }

    @Override
    public List<Book> selectBydate() {
        List<Book> list = bookMapper.selectBydate();
        return list;
    }

    @Override
    public List<Book> select(String title) {
        List<Book> select = bookMapper.select(title);
        return select;
    }

    @Override
    public Book add(Book book) {
        bookMapper.add(book);
       return book;
    }

    @Override
    public int delete(int id) {
        int i = bookMapper.delete(id);
        if(i>-1){
        return 1;
        }else
        return 0;
    }

    @Override
    public Book update(Book book) {
        int update = bookMapper.update(book);
        return book;
    }

    @Override
    public Book search(int id) {
        Book search = bookMapper.search(id);
        return search;
    }
}
